package com.xinbochuang.machine.admin.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinbochuang.machine.admin.domain.Certification;
import com.xinbochuang.machine.admin.domain.system.SysUser;
import com.xinbochuang.machine.admin.mapper.CertificationMapper;
import com.xinbochuang.machine.admin.mapper.system.SysUserMapper;
import com.xinbochuang.machine.admin.service.ICertificationService;

/**
 *
 * @author 杜文博
 * @date 2020-10-09
 */
@Service
public class CertificationServiceImpl extends ServiceImpl<CertificationMapper, Certification> implements ICertificationService {

	@Autowired
	CertificationMapper certificationMapper;

	@Autowired
	SysUserMapper sysUserMapper;

	/**
	 * 查询资质列表
	 * @param certification
	 * @return
	 */
	@Override
	public IPage<Certification> selectList(Certification certification) {
		QueryWrapper<Certification> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(Certification::getDeleteFlag, 0);
		queryWrapper.lambda().orderByAsc(Certification::getUpdateTime);
		if(certification!=null){
			if (StrUtil.isNotBlank(certification.getUserName())) {
				queryWrapper.lambda().like(Certification::getUserName, certification.getUserName());
			}
		}
		return page(new Page<>(certification.getPageNum(), certification.getPageSize()), queryWrapper);
	}

	/**
	 * 新增资质
	 * @param certification
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean insertCertification(Certification certification) {

		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
		certification.setUuid(uuid);
		certification.setDeleteFlag(0);
		certification.setCreateTime(DateUtil.now());
		certification.setUpdateTime(DateUtil.now());
		return save(certification);
	}


	/**
	 * 资质文件修改
	 * @param certification
	 * @return
	 */
	@Override
	public boolean updateCertification(Certification certification) {
		return updateById(certification);
	}

	/**
	 * 批量删除资质
	 * @param uuids
	 * @return
	 */
	@Override
	public boolean deleteCertification(String uuids) {
		String[] uuidList = uuids.split(",", -1);
		Certification certification = new Certification();
		boolean result = false;
		for(String str:uuidList){
			certification=certificationMapper.selectById(str);
			certification.setDeleteFlag(1);
			result = updateById(certification);
		}
		return result;
	}

	@Override
	public Certification getCertById(String uuid) {
		return certificationMapper.selectById(uuid);
	}

	/**
	 * 资质文件下载
	 * @param file
	 * @return
	 * @throws IOException
	 */
	@Override
	public String uploadFile(MultipartFile file) throws IOException {
		// 图片存储路径
		String path = "E:\\fileSave";
		// 判断是否有路径无则生成路径
		if (!new File(path).exists()) {
			new File(path).mkdirs();
		}
		//创建文件名
		String fileName = UUID.randomUUID().toString().replace("-","") + ".jpg";
		//以下为图片本地存储方式
		File tempFile = new File(path,fileName);
		//若文件不存在创建新文件
		if (!tempFile.exists()) {
			tempFile.createNewFile();
		}
		//将内存中图片写入磁盘
		file.transferTo(tempFile);
		return tempFile.toString();
	}


}
